package hp.netty;

import hp.netty.bean.EnvMonitorBean;
import hp.netty.bean.EnvMonitorByteBean;
import io.netty.buffer.ByteBuf;
import org.apache.log4j.Logger;
import util.DataUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Created by 6v on 2019/5/5.
 */
public class Protocol {

    private static Logger log = Logger.getLogger(Protocol.class);

    private byte[] pack;
    public static void main(String[] args) {
        System.out.println((byte)0xf3);
        byte[] ba = {0x04,(byte)0xBA};
        System.out.println(byteToInt2(ba));

        byte[] tail = {0x11,0x7F};
        byte[] b = new byte[63];
        for(int i=0;i<61;i++){
           b[i] = (byte)i;
        }
        System.out.println(b);
       byte[] c =  EnvMonitorByteBean.check(b);
        System.out.println(c);
        System.out.println(DataUtils.byteArrayToInt(tail));
    }





    private EnvMonitorBean reasMsg(Object msg){
        ByteBuf result = (ByteBuf) msg;
        pack = new byte[result.readableBytes()];
        String resultStr = DataUtils.byte2HexStr(pack);

        EnvMonitorByteBean byteBean = new EnvMonitorByteBean();
       if(byteBean.isLegal(pack)){
           EnvMonitorBean bean = new EnvMonitorBean();
           int pos = 0;
           //帧头
           pos += 2;
           //返回字节数
           pos += 1;
//设备温度
           byte[] deviceTemperature = new byte[2];
           System.arraycopy(pack, pos, deviceTemperature, 0, 2);
           bean.setDeviceTemperature(getUnsignedByte(bytesToShort(deviceTemperature))/100);
           pos += 2;
//瞬时流速
           byte[] instanceVelocity = new byte[2];
           System.arraycopy(pack, pos, instanceVelocity, 0, 2);
           bean.setInstanceVelocity(getUnsignedByte(bytesToShort(instanceVelocity))/1000);
           pos += 2;
           //瞬时流速
           byte[] averageVelocity = new byte[2];
           System.arraycopy(pack, pos, averageVelocity, 0, 2);
           bean.setAverageVelocity(getUnsignedByte(bytesToShort(averageVelocity))/1000);
           pos += 2;
           //deviceId
           byte[] deviceId = new byte[8];
           System.arraycopy(pack, pos, deviceId, 0, 8);
           StringBuffer sb = new StringBuffer();
           for(int j=0;j<deviceId.length;j++){
               if(deviceId[j]>9){
                   sb.append(deviceId[j]);
               }else{
                   sb.append("0"+deviceId[j]);
               }

           }
           bean.setDeviceId(sb.toString());
//           bean.setDeviceId(getUnsignedByte(bytesToShort(deviceId)));
           pos += 8;
           //流向
           byte[] flowTo = new byte[2];
           System.arraycopy(pack, pos, flowTo, 0, 2);
           bean.setFlowTo(getUnsignedByte(bytesToShort(flowTo)));
           pos += 2;
           //安装角度
           byte[] installAngle = new byte[2];
           System.arraycopy(pack, pos, installAngle, 0, 2);
           bean.setInstallAngle(getUnsignedByte(bytesToShort(installAngle))/100);
           pos += 2;
           //设备电压
           byte[] voltage = new byte[2];
           System.arraycopy(pack, pos, voltage, 0, 2);
           bean.setVoltage(getUnsignedByte(bytesToShort(voltage)));
           pos += 2;
           //水位高度
           byte[] height = new byte[2];
           System.arraycopy(pack, pos, height, 0, 2);
           bean.setHeight(getUnsignedByte(bytesToShort(height))/1000);
           pos += 2;
           //PH值
           byte[] ph = new byte[2];
           System.arraycopy(pack, pos, ph, 0, 2);
           bean.setPh(getUnsignedByte(bytesToShort(ph))/100);
           pos += 2;
           //浑浊度
           byte[] turbidity = new byte[2];
           System.arraycopy(pack, pos, turbidity, 0, 2);
           bean.setTurbidity(getUnsignedByte(bytesToShort(turbidity))/100);
           pos += 2;
           //电导率
           byte[] conductivity = new byte[2];
           System.arraycopy(pack, pos, conductivity, 0, 2);
           bean.setConductivity(getUnsignedByte(bytesToShort(conductivity))/100);
           pos += 2;
           //溶氧度
           byte[] oxygenDissolved = new byte[2];
           System.arraycopy(pack, pos, oxygenDissolved, 0, 2);
           bean.setOxygenDissolved(getUnsignedByte(bytesToShort(oxygenDissolved))/100);
           pos += 2;
           //水温
           byte[] waterTemperature = new byte[2];
           System.arraycopy(pack, pos, waterTemperature, 0, 2);
           bean.setWaterTemperature(byteToInt2(waterTemperature)/100);
           pos += 2;
           //空气温度
           byte[] airTemperature = new byte[2];
           System.arraycopy(pack, pos, airTemperature, 0, 2);
           bean.setAirTemperature(getUnsignedByte(bytesToShort(airTemperature)));
           pos += 2;
           //空气湿度
           byte[] airHumidity = new byte[2];
           System.arraycopy(pack, pos, airHumidity, 0, 2);
           bean.setAirHumidity(getUnsignedByte(bytesToShort(airHumidity)));
           pos += 2;
           //PM2.5
           byte[] pm25 = new byte[2];
           System.arraycopy(pack, pos, pm25, 0, 2);
           bean.setPm25(getUnsignedByte(bytesToShort(pm25)));
           pos += 2;
           //PM10
           byte[] pm10 = new byte[2];
           System.arraycopy(pack, pos, pm10, 0, 2);
           bean.setPm10(getUnsignedByte(bytesToShort(pm10)));
           pos += 2;
           //二氧化硫含量
           byte[] so2 = new byte[2];
           System.arraycopy(pack, pos, so2, 0, 2);
           bean.setSo2(getUnsignedByte(bytesToShort(so2))/100);
           pos += 2;
           //二氧化氮含量
           byte[] no2 = new byte[2];
           System.arraycopy(pack, pos, no2, 0, 2);
           bean.setNo2(getUnsignedByte(bytesToShort(no2))/100);
           pos += 2;
           //负氧离子含量
           byte[] negativeoygen = new byte[2];
           System.arraycopy(pack, pos, negativeoygen, 0, 2);
           bean.setNegativeoygen(getUnsignedByte(bytesToShort(negativeoygen))/100);
           pos += 2;

           pos += 12;
           byte[] checksum = new byte[2];
           System.arraycopy(pack, pos, checksum, 0, 2);
           bean.setChecksum(getUnsignedByte(bytesToShort(checksum)));
           pos += 2;
           return bean;
       }else{
           log.info("server收到了一条指令，该指令不符合协议的格式："+ resultStr);
       }

        return null;
    }

    public static int byteToInt2(byte[] b) {

        int mask=0xff;
        int temp=0;
        int n=0;
        for(int i=0;i<b.length;i++){
            n<<=8;
            temp=b[i]&mask;
            n|=temp;
        }
        return n;
    }

    public short bytesToShort(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
    }

    public short bytesToInt(byte[] bytes) {
        return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
    }


    public long getUnsignedInt (int data){     //将int数据转换为0~4294967295 (0xFFFFFFFF即DWORD)。
        return data&0x0FFFFFFFFl;
    }

    public int getUnsignedByte (short data){      //将data字节型数据转换为0~65535 (0xFFFF 即 WORD)。
        return data&0x0FFFF;
    }








}
